package com.s59.demo.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.s59.demo.config.MyBloomFilter;
import com.s59.demo.entity.User;
import com.s59.demo.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author baomidou
 * @since 2023-05-08
 */
@RestController
@RequestMapping("/users")
public class UserController {

    @Autowired
    private IUserService userService;
    @Autowired
    private MyBloomFilter myBloomFilter;

    @GetMapping
    public Page<User> getall(User user, int page , int size){
        QueryWrapper queryWrapper = Wrappers.query()
                .like(StringUtils.hasText(user.getName()),"name",user.getName())
                .eq(user.getBmid()!=null,"bmid",user.getBmid());
        return userService.page(Page.of(page,size),queryWrapper);

    }

    @GetMapping("/{id}")
    public User get(@PathVariable Long id){
        if(myBloomFilter.bloomFilter.mightContain(id)){
            return userService.getById(id);
        }else {
            System.out.println("id="+id+"不存在");
            return null;
        }

    }

    @PostMapping
    public User add(@RequestBody User user){
        return userService.saveWithCache(user);
    }

    @DeleteMapping("/{id}")
    public boolean del(@PathVariable Long id){
        return userService.removeById(id);
    }

    @PutMapping
    public User update(@RequestBody User user){
        return userService.updateByIdWithCache(user);
    }

}
